<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h3>混入</h3>
      <p>{{ name }}的年龄是{{ age }}</p>
      <child></child>
    </div>

    <script src="../Vue.js"></script>
    <script>
      // 混入：分发可复用功能
      // 将多个组件的公共都有的一些属性和方法提出来

      // 创建混入对象
      // 所有组件能写的选项都能写，除了el和template

      // 混入对象的内容和组件的内容冲突的时候，按照组件自身的
      // 生命周期是全部执行
      const myMixin = {
        data() {
          return {
            age: 20,
          };
        },
        methods: {
          fn() {
            console.log("fn");
          },
        },
        created() {
          console.log("myMixin created");
        },
      };

      const Child = {
        mixins: [myMixin],
        template: `
          <div>child组件 - {{ age }}</div>
        `,
        created() {
          console.log("child created");
        },
        mounted() {
          this.fn();
        },
      };
      new Vue({
        el: "#app",
        mixins: [myMixin],
        data: {
          name: "zhangsan",
          age: 30,
        },
        components: {
          Child,
        },
      });
    </script>
  </body>
</html>
